/**
 * Created by mhy on 2018/8/7.
 */
import * as API from '../../api'
import * as types from '../mutation-types'
const manage = {
  state: {
    manageList: [],
    manageInfo: {},
    updateManageResult: {},
  },
  getters: {},
  mutations: {
    [types.GET_MANAGE_LIST] (state, data){
      let list = data.list.filter(function(item){
        return item.subtype == 'submanager'
      })
      state.manageList =  state.manageList.concat(list)
    },
    [types.MANAGE_INFO] (state,data){
      state.manageInfo = data
    },
    [types.UPDATE_SUB_MANAGE] (state,data){
      state.updateManageResult = data
    }

  },
  actions: {
    getManageList({commit, state, rootState}, params){
      var prom = API.submanage(params)
      prom.then(function (data) {
        commit(types.GET_MANAGE_LIST, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    updateManage({commit, state, rootState}, params){
      var prom = API.submanage(params)
      prom.then(function (data) {
        commit(types.UPDATE_SUB_MANAGE, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getManageInfo({commit, state, rootState}, params){
      var prom = API.submanage(params)
      prom.then(function (data) {
        commit(types.MANAGE_INFO, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },

  }
}
export default manage
